package com.lansi.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lansi.bean.Key;
import com.lansi.bean.ResponseResult;
import com.lansi.bean.User;
import com.lansi.service.IKeyService;
import com.lansi.service.IUserService;
import com.lansi.service.RegisterCodeGen;



@Controller
public class RegisterCodeGenConttroller extends BaseController{
	//依赖注入
	@Resource
	private IKeyService keyService;
	@Resource
	private IUserService userService;
	//生成注册码接口
	@RequestMapping("/RegisterCodeGen.do")
	@ResponseBody		
	public  Map<String, Object>  RegisterCodeGen(HttpSession session, @RequestBody Map<String,String> map){
		String email = map.get("email");
		String username = map.get("username");
		System.out.println("注册邮箱:"+email);
		System.out.println("管理员名称:"+username);
		User user = userService.getUsername(username);
	//	@SuppressWarnings("unused")
		String sn=null;
		if(user!=null) {
			sn =user.getSn();
		}		
		Map<String,Object> model = new HashMap<String,Object>();
		boolean b =true;
		if(email.contains("@") && email.contains("."))
	  	{
			 b = keyService.checkEmail(email);
	  	}	
		
		if(b || sn==null) {			
			model.put("code", "-1");
			model.put("desc", "邮箱格式不正确/此邮箱已存在");
		}else {			
			String regcode = RegisterCodeGen.register(26,sn);			
			Key key = new Key();
			key.setUsermail(email);
			key.setRegcode(regcode);
			key.setCtime(new Date());
			keyService.register(key);
			model.put("code", "1");
			model.put("regcode", regcode);
		}
	
			return model;
	}
	
	//邮箱-注册码-验证接口
		@RequestMapping("/RegisterValidator.do")
		@ResponseBody
		public  Map<String, Object>  RegisterValidator(HttpSession session, @RequestBody Map<String,String> map){
			String mail=map.get("email");
			String regcode=map.get("regcode");
			System.out.println("验证邮箱:"+mail);
			System.out.println("验证注册码:"+regcode);
			
			Map<String,Object> model = new HashMap<String,Object>();
			try {
				boolean m =keyService.checkEmail(mail);
				boolean r =keyService.checkRegcode(regcode);
				
				if(m && r) {
					model.put("code", "1");
				}else {
					model.put("code", "-1");
					model.put("desc", "此邮箱已存在/注册码不匹配");
				}
			} catch (Exception e) {
				model.put("code", "-1");
				model.put("desc", "此邮箱已存在/注册码不匹配");
			}
			
			return model;			
		}
		
		//查询出所有Key的信息
		@RequestMapping("/getKey.do")
		@ResponseBody
		public ResponseResult<List<Key>> getUser( @RequestBody Map<String,Integer> map){
			Integer page=map.get("page");
			
			System.out.println("查询页数:"+page);
			ResponseResult<List<Key>> rr = new ResponseResult<List<Key>>();
			Integer Keycount =keyService.getKeycount();//查询出数据库注册码总数量
			if(page==null) {
				page=1;
			}
			System.out.println(page);//第几页
			Integer offset = (page-1)*10;//跳过的页数
			System.out.println(offset);
			List<Key> list = keyService.getKey(offset, 10);
			rr.setData(list);
			rr.setState(1);
			rr.setMessage(Keycount.toString());
			return rr;
		}	
		
		//根据邮箱查询信息
		@RequestMapping("/getKeyByEmail.do")
		@ResponseBody
		public ResponseResult<Key> getEmail( @RequestBody Map<String,String> map){
			
			String usermail=map.get("usermail");
			System.out.println("查询邮箱:"+usermail);
			ResponseResult<Key> rr = new ResponseResult<Key>();
			Key key = keyService.getEmail(usermail);
			
			rr.setData(key);
			rr.setState(1);
			rr.setMessage("1");
			return rr;
		}
		
		//处理批量删除Key
		@RequestMapping("/delKey.do")
		@ResponseBody
		public ResponseResult<Void> delKey( @RequestBody Map<String,Integer[]> map){
			
			Integer[] ids=map.get("ids");
			System.out.println("删除注册码个数:"+ids.length);
			ResponseResult<Void> rr = new ResponseResult<Void>();
			try {
				keyService.delKey(ids);
				rr.setState(1);
				rr.setMessage("删除成功");
			} catch (Exception e) {
				rr.setState(-1);
				rr.setMessage("删除失败");
			}
			
			return rr;
		}
		//单个删除
		@RequestMapping("/delKeyById.do")
		@ResponseBody
		public ResponseResult<Void> delKeyById( @RequestBody Map<String,Integer> map){
			Integer id = map.get("id");
			System.out.println("删除注册码id:"+id);
			ResponseResult<Void> rr = new ResponseResult<Void>();
			try {
				keyService.delById(id);
				rr.setState(1);
				rr.setMessage("删除成功");
				return rr;
			} catch (Exception e) {
				rr.setState(-1);
				rr.setMessage("删除失败");
			}
			return rr;
			
		}
	

}
